package com.eelpo.cmmp.uac.pages.action;

import java.util.Arrays;
import java.util.List;

import com.eelpo.cmmp.core.exception.ServiceException;
import com.eelpo.cmmp.core.wicket.component.label.FeedbackLabel;
import com.eelpo.cmmp.core.wicket.page.extensions.SimpleWebPage;
import com.eelpo.cmmp.uac.dao.vo.Action;
import com.eelpo.cmmp.uac.dao.vo.enumed.Status;
import com.eelpo.cmmp.uac.services.IActionManageService;
import com.eelpo.common.util.BlankUtils;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * <pre>
 * 新增和修改动作页面
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public class AddOrEditAction extends SimpleWebPage {

	@SpringBean
	private IActionManageService actionManageService;

	/**
	 * 
	 * 新增动作构造函数
	 * 
	 */
	public AddOrEditAction() {
		this("[新增]动作", new Action(Status.ENABLED));
	}

	/**
	 * 修改动作构造函数
	 * 
	 * @param action
	 *            动作
	 */
	public AddOrEditAction(final Action action) {
		this("[修改]动作", action);
	}

	/**
	 * @param title
	 *            页面标题
	 * @param 动作
	 *            新增或修改的动作
	 */
	public AddOrEditAction(String title, final Action action) {
		super(title);
		add(renderFormContainer(action));
	}

	public Form<Action> renderFormContainer(final Action action) {

		Form<Action> form = new Form<Action>("form", new Model<Action>(action));

		renderFeedbackPanel(form);
		renderActionNameTextField(form);
		renderActionCodeTextField(form);
		renderActionStatusDropDownChoice(form);
		renderSaveButton(form);
		renderBackButton(form);

		return form;
	}

	public void renderFeedbackPanel(Form<Action> form) {
		form.add(renderFeedbackPanel("feedback"));
	}

	public void renderActionNameTextField(Form<Action> form) {
		TextField<String> txtActionName = new TextField<String>("txtActionName", new PropertyModel<String>(form.getDefaultModelObject(), "name"));
		txtActionName.setMarkupId("txtActionName");
		form.add(new FeedbackLabel("txtActionNameFeedback", txtActionName));
		form.add(txtActionName);
	}

	public void renderActionCodeTextField(Form<Action> form) {
		TextField<String> txtActionCode = new TextField<String>("txtActionCode", new PropertyModel<String>(form.getDefaultModelObject(), "code"));
		txtActionCode.setMarkupId("txtActionCode");
		form.add(new FeedbackLabel("txtActionCodeFeedback", txtActionCode));
		form.add(txtActionCode);
	}

	public void renderActionStatusDropDownChoice(Form<Action> form) {
		List<Status> dataList = Arrays.asList(new Status[] { Status.ENABLED, Status.DISABLED });
		IModel<Status> model = new PropertyModel<Status>(form.getDefaultModelObject(), "status");
		IChoiceRenderer<Status> choiceRenderer = new ChoiceRenderer<Status>("label", "value");
		DropDownChoice<Status> actionStatus = new DropDownChoice<Status>("selStatus", model, dataList, choiceRenderer);
		actionStatus.setMarkupId("selStatus");
		form.add(new FeedbackLabel("selStatusFeedback", actionStatus));
		form.add(actionStatus);
	}

	public void renderSaveButton(final Form<Action> form) {
		Button btnSave = new Button("btnSave") {

			private static final long serialVersionUID = 4413085940413146342L;

			public void onSubmit() {
				try {
					Action action = (Action) form.getDefaultModelObject();

					if (BlankUtils.isBlank(action.getId())) {
						actionManageService.addAction(action);
					} else {
						actionManageService.updateAction(action);
					}

					setResponsePage(new ActionManage());
				} catch (ServiceException ex) {
					info(ex.getMessage());
				} catch (Exception e) {
					e.printStackTrace();
					info("保存失败！");
				}
			}
		};

		form.add(btnSave);
	}

	public void renderBackButton(final Form<Action> form) {
		Link<Void> btnBack = new Link<Void>("btnBack") {

			private static final long serialVersionUID = -9129976099407032628L;

			@Override
			public void onClick() {
				setResponsePage(new ActionManage());
			}
		};

		form.add(btnBack);
	}
}
